package com.kamistoat.meimeistore.member.controller;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kamistoat.meimeistore.member.dao.MemberReceiveAddressDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.kamistoat.meimeistore.member.entity.MemberReceiveAddressEntity;
import com.kamistoat.meimeistore.member.service.MemberReceiveAddressService;
import com.kamistoat.common.utils.PageUtils;
import com.kamistoat.common.utils.R;


/**
 * 会员收货地址
 *
 * @author kamistoat
 * @email linjianshanke@163.com
 * @date 2021-09-12 16:36:46
 */
@RestController
@RequestMapping("meimeistoremember/memberreceiveaddress")
public class MemberReceiveAddressController {
    @Autowired
    private MemberReceiveAddressService memberReceiveAddressService;
    @Autowired
    private MemberReceiveAddressDao memberReceiveAddressDao;

    /**
     * 自己写的接口。是一个远程接口。
     * 当购物车结算时，直接把当前用户的所有收货地址查出来返回去
     *
     * @param memberId
     * @return
     */
    @GetMapping("/getOrderAddress")
    public List<MemberReceiveAddressEntity> getOrderAddress(@RequestParam Long memberId) {
        List<MemberReceiveAddressEntity> memberReceiveAddressEntityList =
                memberReceiveAddressDao.selectList(
                        new QueryWrapper<MemberReceiveAddressEntity>().eq("member_id", memberId));
        return memberReceiveAddressEntityList;
    }

    /**
     * 自己接口。获取运费
     * @param attrId
     * @return
     */
    @GetMapping("/fare")
    public R getFare(@RequestParam Long attrId){
        BigDecimal fare = memberReceiveAddressService.getFare(attrId);
        return R.ok().put("fare",fare);
    }

    /**
     * 获取id对应地址
     * @param attrId
     * @return
     */
    @GetMapping("/getAddressId")
    MemberReceiveAddressEntity getAddressId(@RequestParam Long attrId){
        MemberReceiveAddressEntity byId = memberReceiveAddressService.getById(attrId);
        return byId;
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("meimeistoremember:memberreceiveaddress:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberReceiveAddressService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("meimeistoremember:memberreceiveaddress:info")
    public R info(@PathVariable("id") Long id) {
        MemberReceiveAddressEntity memberReceiveAddress = memberReceiveAddressService.getById(id);

        return R.ok().put("memberReceiveAddress", memberReceiveAddress);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("meimeistoremember:memberreceiveaddress:save")
    public R save(@RequestBody MemberReceiveAddressEntity memberReceiveAddress) {
        memberReceiveAddressService.save(memberReceiveAddress);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("meimeistoremember:memberreceiveaddress:update")
    public R update(@RequestBody MemberReceiveAddressEntity memberReceiveAddress) {
        memberReceiveAddressService.updateById(memberReceiveAddress);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("meimeistoremember:memberreceiveaddress:delete")
    public R delete(@RequestBody Long[] ids) {
        memberReceiveAddressService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
